<?php
class ComplaintController extends AppController {

	public $name = 'complaint';
	function beforeFilter() {
	    parent::beforeFilter(); 
		$this->C_auth->autoRedirect = false; //For override login
	    $this->C_auth->allowedActions = array('index','del','typelist','typedel','typeedit','typedoedit');

	}
	
	public function index() {	
        $page = intval(Request::get_args('page'));
        $url_this = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
        $tb = array('model' => 'complaint',
 		            'joins' => array(
					    array('type'=>'left','table'=>'user','conditions'=>array('complaint.user_id' =>'user.id')),
					    array('type'=>'left','table'=>'goods','conditions'=>array('complaint.goods_id' =>'goods.id')),						
						),
				    'fields' => array('complaint.*','user.locked','user.username','goods.lock_flg'),
					'conditions' => array(),
					'order' => '',
					'limit' => '10',
					'group' => '',
					'url' => preg_replace("/&page=([^&]+)/","",$url_this),
					'page' => $page,
					'plist' => 6				
				); 
	    $result =  $this->fpage($tb); 	
        $this->set('result',$result);
	}
	
	public function del($id) {
        $complaints_id = intval($id);
        if(!$complaints_id) { 
		    $this->flash("参数错误！", Request::referer()); 
		}	
		$query = array('keyid'=>$complaints_id);			
        if(POP::exe('[@@:UM complaint][@@:QU del]',$query)	) {		
		    $this->flash('删除失败！', Request::referer());			 
		}else {
		    $this->flash('删除成功！', Request::referer());
        }		
    }	
	
	public function typelist() {
        $page = intval(Request::get_args('page'));
        $url_this = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
        $tb = array('model' => 'complainttype',
				    'fields' => array('complainttype.*'),
					'conditions' => array(),
					'order' => '',
					'limit' => '10',
					'group' => '',
					'url' => preg_replace("/&page=([^&]+)/","",$url_this),
					'page' => $page,
					'plist' => 6				
				); 
	    $result =  $this->fpage($tb); 	
        $this->set('result',$result);	
	}		
	
	public function typeedit() {	
        $type_id=Request::get_args('type_id');
        if($type_id){
			$query = array(
				'conditions' => array('complainttype.id' => $type_id), 
				'fields' => array('complainttype.*'), 
			);
			$result = POP::exe('[@@:UM complainttype][@@:QU find:first]',$query);		
	        $result = $result['complainttype'];
        }else{
	        $result['type_content'] = "";
	        $result['id'] = "";
        }	
        $this->set('result',$result);			
	}		

	public function typedoedit() {	
        /* post 数据处理 */
		$post = array();
        $post['type_content'] = trim(Request::get_args('type_content'));
        $type_id=Request::get_args('type_id');
/*         if(!$type_id) {
		    $this->flash("参数错误！", Router::url('admin/complaint/typelist/')); 
        }	 */	
		$query = array(
			'conditions' => array('complainttype.id <>' => $type_id,'complainttype.type_content'=>$post['type_content']), 
			'fields' => array('complainttype.*'), 
		);
		$result = POP::exe('[@@:UM complainttype][@@:QU find:first]',$query);		
        if(empty($result)) {
            if($type_id!=0){
			    $safeData['complainttype'] = $post;
				$query = array();
				$query['keyid'] = $type_id;
				$query['set'] = $safeData;					
	            if(POP::exe('[@@:UM complainttype][@@:QU save]',$query)) {
			        $array = array(
				        'log_time' => date("Y-m-d H:i:s"),
				        'admin_id' => $this->my->id,
			            'admin_name' => $this->my->username,
				        'log_info' => "修改投诉标题",
			            'ip_address' => Request::getclientIp(),					
			        );
	                $safeData['adminlog'] = $array;								
                    POP::exe('[@@:UM adminlog][@@:QU save]',$safeData);	
		            $this->flash("修改成功！", Router::url('admin/complaint/typelist/'));
	            } else {
		            $this->flash("修改失败！", Router::url('admin/complaint/typelist/'));
	            }
            }else{
			    $safeData['complainttype'] = $post;				
				$query = $safeData;
				if (POP::exe('[@@:UM complainttype][@@:QU save]',$query)) {				
			        $array = array(
				        'log_time' => date("Y-m-d H:i:s"),
				        'admin_id' => $this->my->id,
			            'admin_name' => $this->my->username,
				        'log_info' => "添加投诉标题",
			            'ip_address' => Request::getclientIp(),					
			        );
	                $safeData['adminlog'] = $array;	
                    POP::exe('[@@:UM adminlog][@@:QU save]',$safeData);
		            $this->flash("添加成功！", Router::url('admin/complaint/typelist/'));
	            } else {
		            $this->flash("添加失败！", Router::url('admin/complaint/typelist/'));
	            }
            }		
		} else {
		    $this->flash("投诉标题已经存在！", Router::url('admin/complaint/typelist/'));
		}			
	}		
	
	public function typedel() {
        /* post 数据处理 */
        $type_id=Request::get_args('type_id');
        if($type_id){
/* 	        if(!is_array($type_id)){
			    $type_id=array(intval($type_id));
		    }
	        $type_id=implode(",", $type_id); */
        }else{
	        $type_id = intval(Request::get_args('id'));
        }
        if(!$type_id) {
		    $this->flash("参数错误！", Request::referer()); 
        }
		$query = array('conditions'=>array('complainttype.id' => $type_id));				
        if(POP::exe('[@@:UM complainttype][@@:QU del]',$query)) {
		    $this->flash('删除成功！', Request::referer());			 
		}else {
		    $this->flash('删除失败！', Request::referer());
        }		
    }	
	
}

?>